package com.ecjia.widgets.xlratingbar;

import android.content.Context;

import android.content.res.Resources;

import android.util.DisplayMetrics;

import android.util.TypedValue;



public class UnitUtil {

    @SuppressWarnings("static-access")

    public static float dp2Px(Context context, float value) {

        if (context == null) {

            return 0;

        }

        TypedValue typedValue = new TypedValue();

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();

        return typedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, metrics);

    }



    @SuppressWarnings("static-access")

    public static float sp2Px(Context context, float value) {

        TypedValue typedValue = new TypedValue();

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();

        return typedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value, metrics);

    }



    public static int dpToPx(float dp, Resources resources) {

        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());

        return (int) px;

    }

}